package com.unohacha.qqj.fzdr.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.adapter.SysPushAdapter;
import com.unohacha.qqj.fzdr.bean.BaseBean;
import com.unohacha.qqj.fzdr.bean.SysPuthBean;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SySPushActivity extends BaseActivity {
    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.car_list_recycler)
    RecyclerView carListRecycler;

    SysPushAdapter sysPushAdapter;
    private int mNextRequestPage = 1;
    private static final int PAGE_SIZE = 10;
    private String typeid="1";
    private String id="";
    List<SysPuthBean> listBeans;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_sy_spush;
    }

    @Override
    protected void initData() {
        super.initData();
        commonTitle.setText("系统推送");
        commonRightImage.setVisibility(View.INVISIBLE);
        commonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        listBeans=new ArrayList<>();
        sysPushAdapter=new SysPushAdapter(listBeans);
        carListRecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL,false));

        carListRecycler.setAdapter(sysPushAdapter);
        sysPushAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                Intent intent=new Intent(SySPushActivity.this,PushListActivity.class);
                intent.putExtra("type",listBeans.get(position).getId()+"");
                startActivity(intent);
            }
        });
        getData();
    }

    private void getData(){
        String token = Prefer.getInstance().getToken();
        SuperHttp.post("api/mymessage/systemPushType")
                .addParam("token",token)
                .request(new SimpleCallBack<BaseBean<List<SysPuthBean>>>() {
                    @Override
                    public void onSuccess(BaseBean<List<SysPuthBean>> data) {

                        if(data.getCode()==1){
                            listBeans=data.getData();
                            setData(true,data.getData());
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }

    private void setData(boolean isRefresh,List<SysPuthBean> progect) {

        mNextRequestPage++;
        if (progect == null || progect.size() == 0) {
            return;
        }
        final int size = progect == null ? 0 : progect.size();
        if (isRefresh) {
            sysPushAdapter.setNewData(progect);
        } else {
            if (size > 0) {
                sysPushAdapter.addData(progect);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            sysPushAdapter.loadMoreEnd(isRefresh);
            //   Toast.makeText(this, "no more data", Toast.LENGTH_SHORT).show();
        } else {
            sysPushAdapter.loadMoreComplete();
        }
    }
}
